package com.spring.sunflower.conversation;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.bottompanel.EmotionKeyboard;
import com.spring.sunflower.bottompanel.EmotionUtils;
import com.spring.sunflower.bottompanel.FragmentFactory;
import com.spring.sunflower.bottompanel.GlobalOnItemClickManagerUtils;
import com.spring.sunflower.widget.NoSmoothViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/6/14<br/>
 * <p>
 * Describe:<br/>
 */
public class BottomPanelFragment extends BaseFragment<BottomPanelPresenter> implements View.OnClickListener {
    //是否绑定当前Bar的编辑框的flag
    public static final String BIND_TO_EDITTEXT = "bind_to_edittext";
    //是否隐藏bar上的编辑框和发生按钮
    public static final String HIDE_BAR_EDITTEXT_AND_BTN = "hide bar's editText and btn";

    //当前被选中底部tab
    private static final String CURRENT_POSITION_FLAG = "CURRENT_POSITION_FLAG";
    private int CurrentPosition = 0;
    //是否绑定当前Bar的编辑框,默认true,即绑定。
    //false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
    private boolean isBindToBarEditText = true;

    //是否隐藏bar上的编辑框和发生按钮,默认不隐藏
    private boolean isHidenBarEditTextAndBtn = false;
    private ImageView ivVoice, ivImage, ivVideo, ivEmoji, ivGift;
    private LinearLayout llEmotionLayout;
    //需要绑定的内容view
    private View contentView;
    private EmotionKeyboard mEmotionKeyboard;
    private EditText mEditText;
    List<Fragment> fragments = new ArrayList<>();
    private NoSmoothViewPager noSmoothViewPager;

    public static BottomPanelFragment newInstance(Bundle args) {
        BottomPanelFragment fragment = new BottomPanelFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int setLayoutResId() {
        return R.layout.fragment_bottom_panel;
    }

    @Override
    public void initView() {
        isHidenBarEditTextAndBtn = args.getBoolean(HIDE_BAR_EDITTEXT_AND_BTN);
        //获取判断绑定对象的参数
        isBindToBarEditText = args.getBoolean(BIND_TO_EDITTEXT);
        noSmoothViewPager = mView.findViewById(R.id.bottomViewPager);
        ivVoice = mView.findViewById(R.id.ivVoice);
        ivImage = mView.findViewById(R.id.ivImage);
        ivVideo = mView.findViewById(R.id.ivVideo);
        ivEmoji = mView.findViewById(R.id.ivEmoji);
        ivGift = mView.findViewById(R.id.ivGift);
        ivVoice.setOnClickListener(this);
        ivImage.setOnClickListener(this);
        ivVideo.setOnClickListener(this);
        ivEmoji.setOnClickListener(this);
        ivGift.setOnClickListener(this);
        llEmotionLayout = mView.findViewById(R.id.ll_emotion_layout);
        mEditText = (EditText) mView.findViewById(R.id.etEditInput);
        mEmotionKeyboard = EmotionKeyboard.with(mActivity)
                .setEmotionView(llEmotionLayout)
                .bindToContent(contentView)
                .bindToEditText(!isBindToBarEditText ? ((EditText) contentView) : mEditText)//判断绑定那种EditView
                .bindToEmotionButton(ivEmoji)
                .build();
        GlobalOnItemClickManagerUtils globalOnItemClickManager = GlobalOnItemClickManagerUtils.getInstance(mActivity);
        if (isBindToBarEditText) {
            globalOnItemClickManager.attachToEditText(mEditText);
        } else {
            // false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
            globalOnItemClickManager.attachToEditText((EditText) contentView);
            mEmotionKeyboard.bindToEditText((EditText) contentView);
        }
        mEditText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                View decorView = mActivity.getWindow().getDecorView();
                decorView.getWindowVisibleDisplayFrame(rect);
                int screenHeight = decorView.getRootView().getHeight();
                int diffHeight = screenHeight - rect.bottom;
                boolean visible = diffHeight > screenHeight / 3;
                printD("rectBottom=" + rect.bottom + "---screenHeight=" + screenHeight);
                printD("v=" + visible);
                if (visible) {
                    if (listener != null) {
                        listener.onKeyboardShow();
                    }
                } else {
                    printD("xxx");
                }
            }
        });
        FragmentFactory factory = FragmentFactory.getSingleFactoryInstance();
        EmotionFragment f1 = (EmotionFragment) factory.getFragment(EmotionUtils.EMOTION_CLASSIC_TYPE);
        fragments.add(f1);
        BottomPanelAdapter bottomPanelAdapter = new BottomPanelAdapter(mActivity.getSupportFragmentManager(), fragments);
        noSmoothViewPager.setAdapter(bottomPanelAdapter);
    }

    /**
     * 绑定内容view
     *
     * @param contentView
     * @return
     */
    public void bindToContentView(View contentView) {
        this.contentView = contentView;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {

    }

    @Override
    public BottomPanelPresenter setPresenter() {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivVoice:
                break;
            case R.id.ivEmoji:
                break;
            default:
                break;
        }
    }

    private OnBottomPanelListener listener;

    public interface OnBottomPanelListener {
        void onKeyboardShow();
    }

    public void addOnBottomPanelListener(OnBottomPanelListener listener) {
        this.listener = listener;
    }
}
